package me.shep.wishlist.client;

import java.util.List;

import me.shep.wishlist.client.RPC.InventoryServiceAsync;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AddItemWidget extends Composite {
	Person person;
	Person user;
	DesiredItem newItem = new DesiredItem();
	
	TextBox name = new TextBox();
	TextBox url = new TextBox();
	TextArea description = new TextArea();
	CheckBox purchased = new CheckBox();
	CheckBox show = new CheckBox();
	
	Button addButton;
	InventoryChangedCallback icc;
	public AddItemWidget(Person p, Person u, InventoryChangedCallback icc) {
		person = p;
		user = u;
		this.icc = icc;
		//newItem.setWhoWantsIt(p.getName());
		newItem.setWhoWantsIt(String.valueOf(person.getId()));
		addButton = new Button ("Save", new ClickListener() {
            public void onClick(Widget sender) {
            	newItem.setName(name.getText());
            	newItem.setUrl(url.getText());
            	newItem.setDescription(description.getText());            	
            	newItem.setWanterCanSee(show.isChecked());
            	if(purchased.isChecked())
            		newItem.setWhoPurchasedIt("1");
            	//TODO:  check for purchased, and if purchased put in user name...
            	InventoryServiceAsync.impl.addItem(newItem, Cookies.getCookie("token"), addItemCallback);            	
              }
            });
		
		FlexTable table = new FlexTable();
		
		int i = 0;
		table.setText(i++, 0, "Name of Item:");
		table.setText(i++, 0, "Link:");
		table.setText(i++, 0, "Description");
		if(person.getId() != user.getId()) {
			table.setText(i++, 0, "Already Purchased?");
			table.setText(i++, 0, "Show on their list?");
		}
		
		i = 0;
		table.setWidget(i++, 1, name);
		table.setWidget(i++, 1, url);
		table.setWidget(i++, 1, description);
		if(person.getId() != user.getId()) {
			table.setWidget(i++, 1, purchased);
			table.setWidget(i++, 1, show);
		}
		else
			show.setChecked(true);
		table.setWidget(i++, 1, addButton);
		

		
		initWidget(table);
	}
	AsyncCallback addItemCallback = new AsyncCallback() {
		  public void onFailure(Throwable caught) {
			  Window.alert("There was an error adding the item!\n\n"+caught.getMessage());

	  }
	
	  public void onSuccess(Object arg0) {
		  Window.alert("Item Saved Succesfully");
		  name.setText("");
		  url.setText("");
		  description.setText("");
		  purchased.setChecked(false);
		  show.setChecked(false);
		  icc.addItemCallback(newItem);
		  newItem = new DesiredItem();
		  newItem.setWhoWantsIt(String.valueOf(person.getId()));
	  }
	};
}
